<?php

namespace app\job\service;

use app\admin\service\EchartsService;
use app\admin\service\RatingScaleImportService;
use app\admin\service\ReportService;
use app\admin\service\UserImportService;
use app\model\ConsultOfflineResultModel;
use app\model\ConsultOnlineResultModel;
use app\model\EvaluationLogModel;
use app\model\group_activity\GroupActivityTaskUserResultModel;
use app\model\LoginLogModel;
use app\model\LogModel;
use app\model\MessageModel;
use app\model\platform\PlatformPushMessageModel;
use app\model\ProxyEvaluationResultModel;
use app\model\PushMessageModel;
use app\model\RatingScaleLogModel;
use app\model\RatingScaleModel;
use app\model\school\SchoolCourseChapterResultModel;
use app\model\SelfEvaluationResultModel;
use app\model\TaskUserResultContinuityModel;
use app\model\TaskUserResultModel;
use app\model\training\TrainingTaskUserResultModel;
use app\model\training\TrainingTextbookModel;
use app\model\UserModel;
use app\model\WorkersTaskUserResultContinuityModel;
use app\model\WorkersTaskUserResultModel;
use app\user\service\RatingScaleService;
use app\workers\service\RatingScaleService as WorkersRatingScaleService;
use think\Service;
use think\swoole\Websocket;
use app\socket\Server;

class CronJobService extends Service
{
    public string $message;
    public int $line;

    /**
     * 导入量表
     * @param array $data
     * @param int $channel
     * @return bool
     */
    public function RatingScaleImport(array $data, int $channel): bool
    {
        try {
            $RatingScaleImportService = new RatingScaleImportService($this->app);
            foreach ($data as $item) {
                if (!$RatingScaleImportService->import($item['file'], $item['data'], $channel))
                    throw new \Exception($RatingScaleImportService->message . 'line:' . $RatingScaleImportService->line);
            }
            return true;
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            $this->line = $e->getLine();
            return false;
        }
    }

    /**
     * 导入用户
     * @param array $data
     * @return bool
     */
    public function UserImport(array $data): bool
    {
        try {
            $UserImportService = new UserImportService($this->app);
            if (!$UserImportService->import($data['file'], $data['data']))
                throw new \Exception($UserImportService->message . 'line:' . $UserImportService->line);
            return true;
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            $this->line = $e->getLine();
            return false;
        }
    }

    /**
     * 生成报告
     * @param array $data
     * @param int $channel
     * @return bool
     */
    public function generateReport(array $data, int $channel): bool
    {
        try {
            $RatingScaleService = new RatingScaleService($this->app);
            if (!$RatingScaleService->generateReport($data, $channel))
                throw new \Exception($RatingScaleService->message . 'line:' . $RatingScaleService->line);
            return true;
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            $this->line = $e->getLine();
            return false;
        }
    }

    /**
     * 生成报告
     * @param array $data
     * @param int $channel
     * @return bool
     */
    public function generateWorkersReport(array $data, int $channel): bool
    {
        try {
            $RatingScaleService = new WorkersRatingScaleService($this->app);
            if (!$RatingScaleService->generateReport($data, $channel))
                throw new \Exception($RatingScaleService->message . 'line:' . $RatingScaleService->line);
            return true;
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            $this->line = $e->getLine();
            return false;
        }
    }


    /**
     * 推送消息
     * @param array $data
     * @return bool
     */
    public function PushMessage(array $data): bool
    {
        try {
            $PushMessageModel = new PushMessageModel();
            $UserModel = new UserModel();
            $MessageModel = new MessageModel();
            $info = $PushMessageModel->find($data['message_id']);
            if (!$info) return true;
            $info->status = 1;
            $info->save();
            $department_id = explode(',', $info['department_id']);
            $uids = $UserModel->where([
                ['department_id', 'in', $department_id],
                ['role_mode', '=', 3]
            ])->column('id');
            $MessageData = [];
            foreach ($uids as $uid) {
                $MessageAddData = [
                    'uid' => $uid,
                    'type' => 23,
                    'title' => '系统推送',
                    'content' => $info['content'],
                    'time' => time()
                ];
//                cache()->rpush(env('REDIS_PROJECT') . 'message_push', json_encode($MessageAddData, true));
                $MessageData[] = $MessageAddData;
            }
            $MessageModel->insertAll($MessageData);
            return true;
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            $this->line = $e->getLine();
            return false;
        }
    }

    /**
     * 推送消息-区域
     * @param array $data
     * @return bool
     */
    public function PlatformPushMessage(array $data): bool
    {
        try {
            $PlatformPushMessageModel = new PlatformPushMessageModel();
            $UserModel = new UserModel();
            $MessageModel = new MessageModel();
            $info = $PlatformPushMessageModel->find($data['message_id']);
            if (!$info) return true;
            $info->status = 1;
            $info->save();
            $unit_id = explode(',', $info['unit_id']);
            $uids = $UserModel->where([
                ['unit_id', 'in', $unit_id],
                ['role_mode', '=', 3]
            ])->column('id');
            $MessageData = [];
            foreach ($uids as $uid) {
                $MessageAddData = [
                    'uid' => $uid,
                    'type' => 23,
                    'title' => '系统推送',
                    'content' => $info['content'],
                    'time' => time()
                ];
//                cache()->rpush(env('REDIS_PROJECT') . 'message_push', json_encode($MessageAddData, true));
                $MessageData[] = $MessageAddData;
            }
            $MessageModel->insertAll($MessageData);
            return true;
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            $this->line = $e->getLine();
            return false;
        }
    }


    /**
     * 登录日志
     * @param array $data
     * @return bool
     */
    public function LoginLog(array $data): bool
    {
        try {
            //登录
            $LoginLogModel = new LoginLogModel();
            $LoginLogModel->insert([
                'company_id' => $data['company_id'],
                'areas_id' => $data['areas_id'],
                'unit_id' => $data['unit_id'],
                'uid' => $data['id'],
                'time' => time()
            ]);
            //消息提醒
            $MessageModel = new MessageModel();
            $MessageAddData = [
                'uid' => $data['id'],
                'type' => 1,
                'title' => '登录提示',
                'content' => '您于' . date('Y-m-d') . '日期' . date('H:i:s') . '时间，登录了本系统。',
                'time' => time()
            ];
            $MessageModel->insert($MessageAddData);
//            cache()->rpush(env('REDIS_PROJECT') . 'message_push', json_encode($MessageAddData, true));
            //系统日志
            $LogModel = new LogModel();
            $LogModel->insert([
                'company_id' => $data['company_id'],
                'areas_id' => $data['areas_id'],
                'unit_id' => $data['unit_id'],
                'uid' => $data['id'],
                'type' => 1,
                'ip' => $data['ip'],
                'behavior' => '登录系统',
                'status' => 1,
                'ua' => $data['ua'],
                'time' => time()
            ]);
            return true;
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            $this->line = $e->getLine();
            return false;
        }
    }

    /**
     * 个人报告（单份）
     * @param $data
     * @return bool
     */
    public function personCharts($data): bool
    {
        try {
            $ReportService = new ReportService($this->app);
            $GenerateChartJob = new GenerateChartJob($this->app);
            switch ($data['type']) {
                case 'self':
                    $reportData = $ReportService->getSelfReportInfo($data['id']);
                    //更新测评状态
                    $RatingScaleEvaluationService = new RatingScaleEvaluationService($this->app);
                    $RatingScaleEvaluationService->self([
                        'rating_scale_id' => $reportData['rating_scale_id'],
                        'is_warning' => $reportData['is_warning'],
                        'uid' => $reportData['uid'],
                        'unit_id' => $reportData['unit_id'],
                        'id' => $reportData['id'],
                    ]);

                    $FactorScore = 'SelfFactorScore';
                    $model = new SelfEvaluationResultModel();
                    break;
                case 'task':
                    $reportData = $ReportService->getTaskReportInfo($data['id']);
                    //更新测评状态
                    $RatingScaleEvaluationService = new RatingScaleEvaluationService($this->app);
                    $RatingScaleEvaluationService->task([
                        'task_id' => $reportData['task_id'],
                        'rating_scale_id' => $reportData['rating_scale_id'],
                        'is_warning' => $reportData['is_warning'],
                        'uid' => $reportData['uid'],
                        'unit_id' => $reportData['unit_id'],
                        'id' => $reportData['id'],
                    ]);

                    $FactorScore = 'TaskFactorScore';
                    $model = new TaskUserResultModel();
                    break;
                case 'proxy':
                    $reportData = $ReportService->getProxyReportInfo($data['id']);
                    //更新测评状态
                    $RatingScaleEvaluationService = new RatingScaleEvaluationService($this->app);
                    $RatingScaleEvaluationService->proxy([
                        'rating_scale_id' => $reportData['rating_scale_id'],
                        'is_warning' => $reportData['is_warning'],
                        'uid' => $reportData['uid'],
                        'unit_id' => $reportData['unit_id'],
                        'id' => $reportData['id'],
                    ]);

                    $FactorScore = 'ProxyFactorScore';
                    $model = new ProxyEvaluationResultModel();
                    break;
                case 'wireless':
                    $reportData = $ReportService->getWirelessTaskReportInfo($data['id']);
                    //更新测评状态
                    $RatingScaleEvaluationService = new RatingScaleEvaluationService($this->app);
                    $RatingScaleEvaluationService->wireless([
                        'rating_scale_id' => $reportData['rating_scale_id'],
                        'is_warning' => $reportData['is_warning'],
                        'uid' => $reportData['uid'],
                        'unit_id' => $reportData['unit_id'],
                        'id' => $reportData['id'],
                    ]);

                    $FactorScore = 'TaskFactorScore';
                    $model = new WorkersTaskUserResultModel();
                    break;
                case 'offline':
                    $reportData = $ReportService->OfflineReportInfo($data['id']);
                    $FactorScore = 'ConsultOfflineFactorScore';
                    $model = new ConsultOfflineResultModel();
                    break;
                case 'online':
                    $reportData = $ReportService->OnlineReportInfo($data['id']);
                    $FactorScore = 'ConsultOnlineFactorScore';
                    $model = new ConsultOnlineResultModel();
                    break;
                case 'group_activity':
                    $reportData = $ReportService->getGroupActivityReportInfo($data['id']);
                    $FactorScore = 'ResultFactorScore';
                    $model = new GroupActivityTaskUserResultModel();
                    break;
                case 'school':
                    $reportData = $ReportService->getSchoolReportInfo($data['id']);
                    $FactorScore = 'ResultFactorScore';
                    $model = new SchoolCourseChapterResultModel();
                    break;
                case 'training':
                    $reportData = $ReportService->getTrainingReportInfo($data['id']);
                    $FactorScore = 'ResultFactorScore';
                    $model = new TrainingTaskUserResultModel();
                    break;
                case 'task_continuity':
                    $reportData = $ReportService->getTaskContinuityReportInfo($data['id']);
                    $FactorScore = 'TaskFactorScoreContinuity';
                    $model = new TaskUserResultContinuityModel();
                    break;
                case 'workers_task':
                    $reportData = $ReportService->getWorkersTaskReportInfo($data['id']);
                    $FactorScore = 'TaskFactorScore';
                    $model = new WorkersTaskUserResultModel();
                    break;
                case 'workers_task_continuity':
                    $reportData = $ReportService->getWorkersTaskContinuityReportInfo($data['id']);
                    $FactorScore = 'TaskFactorScoreContinuity';
                    $model = new WorkersTaskUserResultContinuityModel();
                    break;
                default:
                    throw new \Exception('type类型错误！');
            }
            $barCategory = array_column($reportData[$FactorScore] ?? [], 'factor_name');
            $barValue = array_column($reportData[$FactorScore] ?? [], 'score');
            array_shift($barCategory);
            array_shift($barValue);

            $gaugeMax = $reportData[$FactorScore][0]['high_range'] ?? 0;
            $gaugeValue = $reportData[$FactorScore][0]['score'] ?? 0;

            //柱状图
            if (!empty($barCategory) && !empty($barValue)) {
                $barData = [
                    'animation' => false,
                    'xAxis' => [
                        'type' => 'category',
                        'data' => $barCategory,
                        'axisTick' => [
                            'show' => false
                        ],
                        //设置坐标轴字体颜色和宽度
                        'axisLine' => [
                            //这是y轴文字颜色
                            'lineStyle' => [
                                'color' => "#8999AC"
                            ],
                            'show' => true //y轴线消失
                        ],
                        'axisLabel' => [
                            'color' => "#000",
                            'fontSize' => 10,
                            'rotate' => 45
                        ]
                    ],
                    'yAxis' => [
                        'type' => 'value',
                        //设置坐标轴字体颜色和宽度
                        'axisLine' => [
                            'show' => false //y轴线消失
                        ],
                        // 背景线变虚线
                        'splitLine' => [
                            'show' => true,
                            'lineStyle' => [
                                'type' => "dashed",
                                'color' => "#3D5676"
                            ]
                        ],
                        'axisLabel' => [
                            'color' => "#000", //坐标值得具体的颜色
                            'fontSize' => 13
                        ]
                    ],
                    'series' => [
                        [
                            'type' => 'bar',
                            'data' => $barValue,
                            'barWidth' => 10,
                            'label' => [
                                'show' => true,
                                'color' => "#000",
                                'fontSize' => 10,
                                'position' => "top"
                            ],
                            'itemStyle' => [
                                'color' => '#3398DB',
                            ]
                        ]
                    ]
                ];
                $reportData['barImg'] = $GenerateChartJob->generateChartHtml(json_encode($barData), 600, 300);
            }

            if ($gaugeMax > 0) {
                //仪表盘
                $gaugeData = [
                    'animation' => false,
                    'series' => [
                        [
                            'type' => 'gauge',
                            'center' => ['50%', '50%'],
                            'radius' => '95%',
                            'min' => 0,
                            'max' => $gaugeMax, // 保留原变量
                            'axisLine' => [
                                'lineStyle' => [
                                    'width' => 30,
                                    'color' => [
                                        [0.4, '#67e0e3'],
                                        [0.8, '#37a2da'],
                                        [1, '#fd666d']
                                    ]
                                ]
                            ],
                            'pointer' => [
                                'itemStyle' => [
                                    'color' => 'auto'
                                ],
                                'length' => '75%' // 合并原重复的pointer配置
                            ],
                            'axisTick' => [
                                'distance' => -30,
                                'length' => 8,
                                'lineStyle' => [
                                    'color' => '#12253b',
                                    'width' => 2
                                ]
                            ],
                            'splitLine' => [
                                'distance' => -30,
                                'length' => 30,
                                'lineStyle' => [
                                    'color' => '#12253b',
                                    'width' => 4
                                ]
                            ],
                            'axisLabel' => [
                                'color' => 'inherit',
                                'distance' => 50,
                                'fontSize' => 12
                            ],
                            'detail' => [
                                'valueAnimation' => true,
                                'formatter' => '{value}',
                                'color' => 'inherit',
                                'fontSize' => 18
                            ],
                            'data' => [
                                [
                                    'value' => $gaugeValue // 保留原变量
                                ]
                            ]
                        ]
                    ]
                ];
                $reportData['gaugeImg'] = $GenerateChartJob->generateChartHtml(json_encode($gaugeData), 360, 360);
            }

            $info = $model->find($data['id']);
            if (!$info) throw new \Exception('参数错误！');
            $EchartsService = new EchartsService($this->app);
            //普通报告
            $SelfWord = $EchartsService->SelfWord($reportData, $FactorScore);
            $saveData = [
                'word' => $SelfWord['word'],
                'pdf' => $SelfWord['pdf'],
            ];
            //医院版报告
//            $HospitalWord = $EchartsService->HospitalWord($reportData,$FactorScore);
//            $saveData['word'] =$HospitalWord['word'];
//            $saveData['pdf'] =$HospitalWord['pdf'];
            $info->save($saveData);
            return true;
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            $this->line = $e->getLine();
            return false;
        }
    }

    /**
     * 个人报告（单份）重置报告文件
     * @param $data
     * @return bool
     */
    public function personChartsRecover($data): bool
    {
        try {
            $ReportService = new ReportService($this->app);
            $GenerateChartJob = new GenerateChartJob($this->app);
            switch ($data['type']) {
                case 'self':
                    $reportData = $ReportService->getSelfReportInfo($data['id']);
                    $FactorScore = 'SelfFactorScore';
                    $model = new SelfEvaluationResultModel();
                    break;
                case 'task':
                    $reportData = $ReportService->getTaskReportInfo($data['id']);
                    $FactorScore = 'TaskFactorScore';
                    $model = new TaskUserResultModel();
                    break;
                case 'proxy':
                    $reportData = $ReportService->getProxyReportInfo($data['id']);
                    $FactorScore = 'ProxyFactorScore';
                    $model = new ProxyEvaluationResultModel();
                    break;
                case 'offline':
                    $reportData = $ReportService->OfflineReportInfo($data['id']);
                    $FactorScore = 'ConsultOfflineFactorScore';
                    $model = new ConsultOfflineResultModel();
                    break;
                case 'online':
                    $reportData = $ReportService->OnlineReportInfo($data['id']);
                    $FactorScore = 'ConsultOnlineFactorScore';
                    $model = new ConsultOnlineResultModel();
                    break;
                case 'group_activity':
                    $reportData = $ReportService->getGroupActivityReportInfo($data['id']);
                    $FactorScore = 'ResultFactorScore';
                    $model = new GroupActivityTaskUserResultModel();
                    break;
                case 'school':
                    $reportData = $ReportService->getSchoolReportInfo($data['id']);
                    $FactorScore = 'ResultFactorScore';
                    $model = new SchoolCourseChapterResultModel();
                    break;
                case 'training':
                    $reportData = $ReportService->getTrainingReportInfo($data['id']);
                    $FactorScore = 'ResultFactorScore';
                    $model = new TrainingTaskUserResultModel();
                    break;
                case 'task_continuity':
                    $reportData = $ReportService->getTaskContinuityReportInfo($data['id']);
                    $FactorScore = 'TaskFactorScoreContinuity';
                    $model = new TaskUserResultContinuityModel();
                    break;
                case 'workers_task':
                    $reportData = $ReportService->getWorkersTaskReportInfo($data['id']);
                    $FactorScore = 'TaskFactorScore';
                    $model = new WorkersTaskUserResultModel();
                    break;
                case 'workers_task_continuity':
                    $reportData = $ReportService->getWorkersTaskContinuityReportInfo($data['id']);
                    $FactorScore = 'TaskFactorScoreContinuity';
                    $model = new WorkersTaskUserResultContinuityModel();
                    break;
                default:
                    throw new \Exception('type类型错误！');
            }
            $barCategory = array_column($reportData[$FactorScore] ?? [], 'factor_name');
            $barValue = array_column($reportData[$FactorScore] ?? [], 'score');
            array_shift($barCategory);
            array_shift($barValue);

            $gaugeMax = $reportData[$FactorScore][0]['high_range'] ?? 0;
            $gaugeValue = $reportData[$FactorScore][0]['score'] ?? 0;

            //柱状图
            if (!empty($barCategory) && !empty($barValue)) {
                $barData = [
                    'animation' => false,
                    'xAxis' => [
                        'type' => 'category',
                        'data' => $barCategory,
                        'axisTick' => [
                            'show' => false
                        ],
                        //设置坐标轴字体颜色和宽度
                        'axisLine' => [
                            //这是y轴文字颜色
                            'lineStyle' => [
                                'color' => "#8999AC"
                            ],
                            'show' => true //y轴线消失
                        ],
                        'axisLabel' => [
                            'color' => "#000",
                            'fontSize' => 10,
                            'rotate' => 45
                        ]
                    ],
                    'yAxis' => [
                        'type' => 'value',
                        //设置坐标轴字体颜色和宽度
                        'axisLine' => [
                            'show' => false //y轴线消失
                        ],
                        // 背景线变虚线
                        'splitLine' => [
                            'show' => true,
                            'lineStyle' => [
                                'type' => "dashed",
                                'color' => "#3D5676"
                            ]
                        ],
                        'axisLabel' => [
                            'color' => "#000", //坐标值得具体的颜色
                            'fontSize' => 13
                        ]
                    ],
                    'series' => [
                        [
                            'type' => 'bar',
                            'data' => $barValue,
                            'barWidth' => 10,
                            'label' => [
                                'show' => true,
                                'color' => "#000",
                                'fontSize' => 10,
                                'position' => "top"
                            ],
                            'itemStyle' => [
                                'color' => '#3398DB',
                            ]
                        ]
                    ]
                ];
                $reportData['barImg'] = $GenerateChartJob->generateChartHtml(json_encode($barData), 600, 300);
            }

            if ($gaugeMax > 0) {
                //仪表盘
                $gaugeData = [
                    'animation' => false,
                    'series' => [
                        [
                            'type' => 'gauge',
                            'center' => ['50%', '50%'],
                            'radius' => '95%',
                            'min' => 0,
                            'max' => $gaugeMax, // 保留原变量
                            'axisLine' => [
                                'lineStyle' => [
                                    'width' => 30,
                                    'color' => [
                                        [0.4, '#67e0e3'],
                                        [0.8, '#37a2da'],
                                        [1, '#fd666d']
                                    ]
                                ]
                            ],
                            'pointer' => [
                                'itemStyle' => [
                                    'color' => 'auto'
                                ],
                                'length' => '75%' // 合并原重复的pointer配置
                            ],
                            'axisTick' => [
                                'distance' => -30,
                                'length' => 8,
                                'lineStyle' => [
                                    'color' => '#12253b',
                                    'width' => 2
                                ]
                            ],
                            'splitLine' => [
                                'distance' => -30,
                                'length' => 30,
                                'lineStyle' => [
                                    'color' => '#12253b',
                                    'width' => 4
                                ]
                            ],
                            'axisLabel' => [
                                'color' => 'inherit',
                                'distance' => 50,
                                'fontSize' => 12
                            ],
                            'detail' => [
                                'valueAnimation' => true,
                                'formatter' => '{value}',
                                'color' => 'inherit',
                                'fontSize' => 18
                            ],
                            'data' => [
                                [
                                    'value' => $gaugeValue // 保留原变量
                                ]
                            ]
                        ]
                    ]
                ];
                $reportData['gaugeImg'] = $GenerateChartJob->generateChartHtml(json_encode($gaugeData), 360, 360);
            }

            $info = $model->find($data['id']);
            if (!$info) throw new \Exception('参数错误！');
            $EchartsService = new EchartsService($this->app);
            //普通报告
            $SelfWord = $EchartsService->SelfWord($reportData, $FactorScore);
            $saveData = [
                'word' => $SelfWord['word'],
                'pdf' => $SelfWord['pdf'],
            ];
            //医院版报告
//            $HospitalWord = $EchartsService->HospitalWord($reportData,$FactorScore);
//            $saveData['word'] =$HospitalWord['word'];
//            $saveData['pdf'] =$HospitalWord['pdf'];
            $info->save($saveData);
            return true;
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            $this->line = $e->getLine();
            return false;
        }
    }

    /**
     * 量表测评记录
     * @param $param
     * @return bool
     */
    public function evaluationLog($param): bool
    {
        try {
            $data = $param['data'];
            $channel = $param['channel'];
            $RatingScaleModel = new RatingScaleModel();
            $rating_scale_name = $RatingScaleModel->where('id','=',$data['rating_scale_id'])->value('name');
            //更新用户列表【用户档案更新时间】
            $UserModel = new UserModel();
            $UserModel->where('id','=', $data['uid'])->update(['archive_update_time' => time(), 'is_normal' => 1]);
            //系统日志
            $LogModel = new LogModel();
            $LogModel->insert([
                'company_id' => $data['company_id'],
                'areas_id' => $data['areas_id'],
                'unit_id' => $data['unit_id'],
                'uid' => $data['uid'],
                'type' => 3,
                'ip' => getIp(),
                'behavior' => '测评结果生成：' . $rating_scale_name,
                'status' => 1,
                'ua' => request()->server('HTTP_USER_AGENT'),
                'time' => time()
            ]);
            //量表测评记录
            $RatingScaleLogModel = new RatingScaleLogModel();
            $RatingScaleLogModel->insert([
                'unit_id' => $data['unit_id'],
                'uid' => $data['uid'],
                'rating_scale_id' => $data['rating_scale_id'],
                'source' => $channel,
                'time' => time()
            ]);
            $EvaluationLogModel = new EvaluationLogModel();
            $EvaluationLogModel->insert([
                'unit_id' => $data['unit_id'],
                'uid' => $data['uid'],
                'rating_scale_id' => $data['rating_scale_id'],
                'type' => $channel,
                'finish_used_time' => $data['finish_used_time'],
                'time' => time()
            ]);
            return true;
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            $this->line = $e->getLine();
            return false;
        }
    }

    /**
     * 教材ppt转pdf
     * @param $param
     * @return bool
     */
    public function textbookConvertToPDF($param): bool
    {
        try {
            $TrainingTextbookModel = new TrainingTextbookModel();
            $info = $TrainingTextbookModel->find($param['id']);
            if (!$info) throw new \Exception('未找到教材记录！');
            switch (env('UPLOAD_TYPE')){
                case 'local':
                    $param['path2'] = substr($param['path'],1);
                    if (!file_exists(app()->getRootPath().'public'.$param['path2'])) {
                        throw new \Exception('文件不存在！');
                    }
                    WordToPdf(app()->getRootPath().'public'.$param['path2']);
                    $ext = pathinfo(app()->getRootPath().'public'.$param['path2'], PATHINFO_EXTENSION);
                    $path = str_replace($ext, 'pdf', $param['path']);
                    break;
                case 'oss':
                    $ext = '.'.pathinfo($param['path'], PATHINFO_EXTENSION);
                    $save_name = '/upload/'.date('Ym').'/'.date('d').'_'.time().uniqid();
                    $file_path  = dirname(app()->getRootPath().'public'.$save_name.$ext);
                    folder_build($file_path);
                    if (!file_put_contents(app()->getRootPath().'public'.$save_name . $ext, file_get_contents($param['path']))) throw new \Exception('文件加载失败！');
                    WordToPdf(app()->getRootPath().'public'.$save_name . $ext);
                    $path = app()->getRootPath().'public'.$save_name.'.pdf';
                    $path = oss($path,'.pdf');
                    break;
                default:
                    throw new \Exception('系统错误！');
            }
            $info->save([
                'pdf' => $path
            ]);
            return true;
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            $this->line = $e->getLine();
            return false;
        }
    }

}